package dhm.com.dhmshop.fragment.shoppingcart;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.MyShopsAdapter;
import dhm.com.dhmshop.adapter.order.ParamTypeAdapter;
import dhm.com.dhmshop.base.BaseFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetGoodsFreightBean;
import dhm.com.dhmshop.entity.MyShops;
import dhm.com.dhmshop.entity.ShopCarBean;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.framework.module.commodity.entity.OneGoodsEntity;
import dhm.com.dhmshop.utils.CustomJiaJian;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.home.OrderdetailActivity;
import dhm.com.dhmshop.view.main.LoginActivity;
import dhm.com.dhmshop.view.main.MainActivity;
import dhm.com.dhmshop.view.mine.myshop.MyshopActivity;
import dhm.com.dhmshop.view.mine.myshop.ShopdetailActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingcartFragment extends BaseFragment implements LoginContract.IView {

    @BindView(R.id.chenge)
    CheckBox chenge;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.allcheck)
    CheckBox allcheck;
    @BindView(R.id.heji)
    TextView heji;
    @BindView(R.id.btn_jie)
    TextView btn_jie;
    @BindView(R.id.lin)
    LinearLayout lin;
    @BindView(R.id.linearimage)
    LinearLayout image;
    @BindView(R.id.lindel)
    LinearLayout lindel;
    Unbinder unbinder;
    private View rootView;
    private MyShopsAdapter myShopsAdapter;
    private PressenterImpl pressenter;
    private String uid;
    private MyShops myShops;
    private OneGoodsEntity oneGoodsEntit;
    private int shopFreight;
    private int goodsFreight;

    @Override
    protected int getLayout() {
        return R.layout.fragment_shoppingcart;
    }

    @Override
    protected void initView() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(getActivity(), "uid");
        if (uid == null || uid.equals("")) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            getActivity().startActivity(intent);
            getActivity().finish();
        }
    }

    @Override
    protected void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        manager.setStackFromEnd(true);
//        manager.setReverseLayout(true);

        myShopsAdapter = new MyShopsAdapter(getActivity());
        myShopsAdapter.setdeleteClick(new MyShopsAdapter.OnClick() {
            @Override
            public void item(int posttion, boolean checked) {
                myShops.getData().get(posttion).setChecked(checked);
                myShopsAdapter.notifyDataSetChanged();
                boolean checkchecked = checkchecked();
                allcheck.setChecked(checkchecked);
                allprice();
            }
        });
        myShopsAdapter.setaddClick(new MyShopsAdapter.OnIntentClick() {
            @Override
            public void item(int posttion, int num) {
//                goods_formats=myShops.getData().get(posttion).getGoods_formats();
//                Map<String,String> map=new HashMap<>();
//                map.put("token",Constant.TOKEN);
//                map.put("shopping_id",myShops.getData().get(posttion).getId()+"");
//                map.put("uid",uid);
//                map.put("num",num+"");
//                map.put("goods_formats",goods_formats);
//                pressenter.sendMessage(getActivity(),Constant.ModifyShopping,map,Bean.class);

                myShops.getData().get(posttion).getList().get(0).setCartId(num);
                myShopsAdapter.notifyDataSetChanged();
                allprice();
            }
        });

        myShopsAdapter.settiemClick(new MyShopsAdapter.OnShopItemClick() {
            @Override
            public void item(int posttion, MyShops.DataBean databean) {
                myShops.getData().set(posttion, databean);
                boolean checkchecked = checkchecked();
                allcheck.setChecked(checkchecked);
                allprice();
            }
        });

        myShopsAdapter.settiemchange(new MyShopsAdapter.OnShopItemChange() {
            @Override
            public void item(int i, int posttion, MyShops.DataBean databean) {
                myShops.getData().set(i, databean);
                boolean checkchecked = checkchecked();
                allcheck.setChecked(checkchecked);
                allprice();

                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid",uid);
                map.put("shopping_id",databean.getList().get(posttion).getCartId()+"");
                map.put("goods_id",databean.getList().get(posttion).getGoodsId()+"");
                map.put("num",databean.getList().get(posttion).getCartNum()+"");
                pressenter.sendMessage(getActivity(),Constant.ChangeShoppingNum,map,Bean.class);
            }
        });


        allcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (myShops.getData()==null||myShops.getData().size()==0){
                    return;
                }
                if (compoundButton.isPressed()) {
                    for (int i = 0; i < myShops.getData().size(); i++) {
                        myShops.getData().get(i).setChecked(allcheck.isChecked());
                        for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                            myShops.getData().get(i).getList().get(j).setChecked(allcheck.isChecked());
                        }
                    }
                    myShopsAdapter.notifyDataSetChanged();
                    allprice();
                }
            }
        });

        recy.setLayoutManager(manager);
        recy.setAdapter(myShopsAdapter);
        getshop();

    }

    private void getshop() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        pressenter.sendMessage(getActivity(), Constant.ShoppingList, map, MyShops.class);
    }

    private boolean checkchecked() {
        if (myShops == null || myShops.getData().size() == 0) {
            return false;
        }
        for (int i = 0; i < myShops.getData().size(); i++) {
            if (!myShops.getData().get(i).isChecked()) {
                return false;
            }
        }
        return true;
    }

    private void allprice() {
        float price = 0;
        for (int i = 0; i < myShops.getData().size(); i++) {
            for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                if (myShops.getData().get(i).getList().get(j).isChecked()) {
                    price = price + myShops.getData().get(i).getList().get(j).getShopPrice() * myShops.getData().get(i).getList().get(j).getCartNum();
                }
            }
        }
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String resultString = decimalFormat.format(price);
        heji.setText("合计：￥" + resultString);
    }

    private String allchecked() {
        String price = "";

        if (myShops ==null || myShops.getData().size()== 0){
            return "";
        }
        for (int i = 0; i < myShops.getData().size(); i++) {
            for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                if (myShops.getData().get(i).getList().get(j).isChecked()) {
                    price = price + myShops.getData().get(i).getList().get(j).getCartId() + ",";
                }
            }
        }

        if (price.equals("")){
            Toast.makeText(getActivity(), "请选择", Toast.LENGTH_SHORT).show();
            return "";
        }
        price = price.substring(0, price.length() - 1);

        return price;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_shoppingcart, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    String shopping_id = "";
    String shopping_ids = "";
    String is_check = "0";

    @OnClick({R.id.chenge, R.id.btn_jie, R.id.btn_del, R.id.btn_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chenge:
                if (chenge.isChecked()) {
                    chenge.setText("完成");
                    chenge.setTextColor(getResources().getColor(R.color.main));
                    lin.setVisibility(View.GONE);
                    lindel.setVisibility(View.VISIBLE);
                } else {
                    chenge.setText("编辑");
                    chenge.setTextColor(getResources().getColor(R.color.mainblack));
                    lin.setVisibility(View.VISIBLE);
                    lindel.setVisibility(View.GONE);
                }
                break;
            case R.id.btn_clear:
                Map<String, String> map1 = new HashMap<>();
                map1.put("token", Constant.TOKEN);
                map1.put("uid", uid);
                pressenter.sendMessage(getActivity(), Constant.DelAllShopping, map1, Bean.class);
                break;
            case R.id.btn_del:
                String allchecked = allchecked();
                if (allchecked.equals("")) {
                    Toast.makeText(getActivity(), "请选择", Toast.LENGTH_SHORT).show();
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("shopping_id", allchecked);
                map.put("uid", uid);
                pressenter.sendMessage(getActivity(), Constant.DelShopping, map, Bean.class);

                break;
            case R.id.btn_jie:
                break;
            default:
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        getshop();
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof MyShops) {
            myShops = (MyShops) data;
            if (myShops.getCode()==1){
                for (int i = 0; i < myShops.getData().size(); i++) {
                    for (int j = i + 1; j < myShops.getData().size(); j++) {
                        if (myShops.getData().get(i).getShopId() == myShops.getData().get(j).getShopId()) {
                            myShops.getData().get(i).addList(myShops.getData().get(j).getList());
                            myShops.getData().remove(j);
                            j--;
                        }
                    }
                }
                myShopsAdapter.setShopList(myShops.getData());
                allprice();
                allcheck.setChecked(checkchecked());
                shopping_ids = "";
                for (int i = 0; i < myShops.getData().size(); i++) {
                    for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                        if (myShops.getData().get(i).getList().get(j).getIsCheck() == 1) {
                            shopping_ids = shopping_ids + myShops.getData().get(i).getList().get(j).getCartId() + ";";
                        }
                    }
                }
                if (!shopping_ids.equals("")){
                    shopping_ids = shopping_ids.substring(0, shopping_ids.length() - 1);
                    is_check = "0";
                    Map<String, String> map2 = new HashMap<>();
                    map2.put("token", Constant.TOKEN);
                    map2.put("shopping_id", shopping_ids);
                    map2.put("uid", uid);
                    map2.put("is_check", "0");
                    pressenter.sendMessage(getActivity(), Constant.ShoppingCkeck, map2, Bean.class);
                }
                shopFreight = 0;
                goodsFreight = 0;
                setGoodsFreight();
                if (myShops.getData()==null||myShops.getData().size()==0){
                    image.setVisibility(View.VISIBLE);
                    recy.setVisibility(View.GONE);
                }else {
                    image.setVisibility(View.GONE);
                    recy.setVisibility(View.VISIBLE);
                }
            }else {
                image.setVisibility(View.VISIBLE);
                recy.setVisibility(View.GONE);
            }
        }else if (data instanceof GetGoodsFreightBean){
            GetGoodsFreightBean getGoodsFreightBean= (GetGoodsFreightBean) data;
            myShops.getData().get(shopFreight).getList().get(goodsFreight).setGoodsFreight(getGoodsFreightBean.getData()+"");
            if (goodsFreight==myShops.getData().get(shopFreight).getList().size()-1){
                goodsFreight=0;
                if (shopFreight!=myShops.getData().size()-1){
                    shopFreight++;
                    setGoodsFreight();
                }else {
                    btn_jie.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            shopping_id = "";
                            for (int i = 0; i < myShops.getData().size(); i++) {
                                for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                                    if (myShops.getData().get(i).getList().get(j).isChecked()) {
                                        shopping_id = shopping_id + myShops.getData().get(i).getList().get(j).getCartId() + ",";
                                    }
                                }
                            }

                            if (shopping_id.equals("")) {
                                Toast.makeText(getActivity(), "请选择商品", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            shopping_id = shopping_id.substring(0, shopping_id.length() - 1);

                            is_check = "1";
                            Map<String, String> map2 = new HashMap<>();
                            map2.put("token", Constant.TOKEN);
                            map2.put("shopping_id", shopping_id);
                            map2.put("uid", uid);
                            map2.put("is_check", "1");

                            pressenter.sendMessage(getActivity(), Constant.ShoppingCkeck, map2, Bean.class);
                        }
                    });
                }
            }else {
                goodsFreight++;
                setGoodsFreight();
            }

        }else if (data instanceof Bean) {
            Bean bean = (Bean) data;
            if (bean.getCode()==1){
                if (bean.getMessage().equals("操作成功")) {
                    if (is_check.equals("1")) {
                        List<ShopCarBean> shopCarBeans = new ArrayList<>();
                        for (int i = 0; i < myShops.getData().size(); i++) {
                            boolean has = false;
                            ShopCarBean shopCarBean = new ShopCarBean();
                            float allprice = 0;
                            float yunfei=0;
                            for (int j = 0; j < myShops.getData().get(i).getList().size(); j++) {
                                if (myShops.getData().get(i).getList().get(j).isChecked()) {
                                    if (!has) {
                                        has = true;
                                    }
                                    ShopCarBean.GoodsBeans goodsBeans = new ShopCarBean.GoodsBeans();
                                    goodsBeans.setCount(myShops.getData().get(i).getList().get(j).getCartNum() + "");
                                    goodsBeans.setGoods_id(myShops.getData().get(i).getList().get(j).getGoodsId() + "");
                                    goodsBeans.setGoods_image(myShops.getData().get(i).getList().get(j).getGoodsImg());
                                    goodsBeans.setName(myShops.getData().get(i).getList().get(j).getGoodsName());
                                    goodsBeans.setPrice(myShops.getData().get(i).getList().get(j).getShopPrice() + "");
                                    shopCarBean.addGoodsbean(goodsBeans);
                                    if (myShops.getData().get(i).getList().get(j).getIsFreeShipping()!=1){
                                        if (myShops.getData().get(i).getList().get(j).getShopExpressId()==0){
                                            allprice = allprice +(myShops.getData().get(i).getList().get(j).getShopPrice() * myShops.getData().get(i).getList().get(j).getCartNum());
                                        }else {
                                            yunfei=yunfei+Float.parseFloat(myShops.getData().get(i).getList().get(j).getGoodsFreight());
                                            allprice = allprice +Float.parseFloat(myShops.getData().get(i).getList().get(j).getGoodsFreight())+
                                                    (myShops.getData().get(i).getList().get(j).getShopPrice() * myShops.getData().get(i).getList().get(j).getCartNum());
                                        }

                                    }else {
                                        allprice = allprice +(myShops.getData().get(i).getList().get(j).getShopPrice() * myShops.getData().get(i).getList().get(j).getCartNum());
                                    }
                                }
                            }
                            if (has) {
                                shopCarBean.setShopName(myShops.getData().get(i).getShopName());
                                shopCarBean.setPeisong("快递运输");
                                shopCarBean.setHeji(allprice + "");
                                shopCarBean.setYunfei(yunfei+"");
                                shopCarBeans.add(shopCarBean);
                            }

                        }
                        Intent intent = new Intent(getActivity(), OrderdetailActivity.class);
                        intent.putExtra("shopCarBean", (Serializable) shopCarBeans);
                        getActivity().startActivity(intent);
                    }
                }else {
                    Toast.makeText(getActivity(), ((Bean) data).getMessage(), Toast.LENGTH_SHORT).show();
                    getshop();
                    ((MainActivity)getActivity()).initdata();
                }
            }


        } else if (data instanceof OneGoodsEntity) {
            OneGoodsEntity oneGoodsEntity = (OneGoodsEntity) data;
//            if (oneGoodsEntity != null || oneGoodsEntity.getData().getComment().size() > 0) {
//                oneGoodsEntit = oneGoodsEntity;
//                showPopwindow(shopping_id);
//            }
        }
    }

    private void setGoodsFreight() {
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("shop_id", myShops.getData().get(shopFreight).getList().get(goodsFreight).getShopId()+"");
            map.put("shippingFeeType", myShops.getData().get(shopFreight).getList().get(goodsFreight).getShippingFeeType()+"");
            map.put("goodsNum", num + "");
            map.put("goodsWeight", myShops.getData().get(shopFreight).getList().get(goodsFreight).getGoodsWeight());
            map.put("goodsVolume", myShops.getData().get(shopFreight).getList().get(goodsFreight).getGoodsVolume());
            if (myShops.getData().get(shopFreight).getList().get(goodsFreight).getShopExpressId()==0){
                map.put("shopExpressId", "1");
            }else {
                map.put("shopExpressId", myShops.getData().get(shopFreight).getList().get(goodsFreight).getShopExpressId()+"");
            }
            pressenter.sendMessage(getActivity(), Constant.GetGoodsFreight, map, GetGoodsFreightBean.class);
    }

    @Override
    public void fail(String error) {

    }

    private void getdetail(String gid) {

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("gid", gid + "");
        map.put("uid", uid + "");
        pressenter.sendMessage(getActivity(), Constant.OneGoods, map, OneGoodsEntity.class);
    }

    private PopupWindow popWindow;
    private int num = 1;
    private int position = 0;
    private String goods_formats = "";

    private void showPopwindow(String shopping_id) {
        View popView = View.inflate(getActivity(), R.layout.popup_param, null);
        ImageView cancle = popView.findViewById(R.id.cancle);
        SimpleDraweeView image = popView.findViewById(R.id.image);
        TextView commde_price = popView.findViewById(R.id.commde_price);
        TextView param = popView.findViewById(R.id.param);
        RecyclerView recy = popView.findViewById(R.id.recy);
        Button sure = popView.findViewById(R.id.sure);
        CustomJiaJian jiajian = popView.findViewById(R.id.jiajian);
        jiajian.setCustomListener(new CustomJiaJian.CustomListener() {
            @Override
            public void jiajian(int count) {
                num = count;
//                commde_price.setText(count*Float.parseFloat(oneGoodsEntit.getData().getExpress_price())+"");
            }

            @Override
            public void shuRuZhi(int count) {
                num = count;
//                commde_price.setText(count*Float.parseFloat(oneGoodsEntit.getData().getExpress_price())+"");
            }
        });

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goods_formats = "";
//                for (int i=0;i<oneGoodsEntit.getData().getParam().size();i++){
//                    for (int j=0;j<oneGoodsEntit.getData().getParam().get(i).getParam_value().size();j++){
//                        if (oneGoodsEntit.getData().getParam().get(i).getParam_check().get(j).equals("1")){
//                            goods_formats=goods_formats+oneGoodsEntit.getData().getParam().get(i).getParam_value().get(j)+",";
//                        }
//                    }
//                }
                myShops.getData().get(position).getList().get(0).setCartNum(num);
                if (goods_formats.equals("")) {
                    Toast.makeText(getActivity(), "请选择商品规格", Toast.LENGTH_SHORT).show();
                    return;
                } else {
//                    myShops.getData().get(position).setGoods_formats(goods_formats);
                }
                goods_formats = goods_formats.substring(0, goods_formats.length() - 1);
                allprice();
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("shopping_id", shopping_id);
                map.put("uid", uid);
                map.put("num", num + "");
                map.put("goods_formats", goods_formats);
                pressenter.sendMessage(getActivity(), Constant.ModifyShopping, map, Bean.class);
                myShopsAdapter.setShopList(myShops.getData());
                popWindow.dismiss();
            }
        });

//        commde_price.setText(oneGoodsEntit.getData().getExpress_price());
//        image.setImageURI(Constant.PATH+"/"+oneGoodsEntit.getData().getGoods_images_thumb());
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        jiajian.setEditText(myShops.getData().get(position).getList().get(0).getCartNum());
        recy.setLayoutManager(manager);
        ParamTypeAdapter paramTypeAdapter = new ParamTypeAdapter(getActivity());
        paramTypeAdapter.setaddClick(new ParamTypeAdapter.OnIntentClick() {
            @Override
            public void item(int posttion, int childposttion) {
//                for (int j=0;j<oneGoodsEntit.getData().getParam().get(posttion).getParam_value().size();j++){
//                    oneGoodsEntit.getData().getParam().get(posttion).getParam_check().set(j,"0");
//                }
//                oneGoodsEntit.getData().getParam().get(posttion).getParam_check().set(childposttion,"1");
                String yixuan = "已选：";
//                for (int i=0;i<oneGoodsEntit.getData().getParam().size();i++){
//                    for (int j=0;j<oneGoodsEntit.getData().getParam().get(i).getParam_value().size();j++){
//                        if (oneGoodsEntit.getData().getParam().get(i).getParam_check().get(j).equals("1")){
//                            yixuan=yixuan+" '"+oneGoodsEntit.getData().getParam().get(i).getParam_value().get(j)+"'";
//                        }else {
//                        }
//                    }
//                }
                param.setText(yixuan);
//                paramTypeAdapter.setImageList(oneGoodsEntit.getData().getParam());
            }
        });
        recy.setAdapter(paramTypeAdapter);
//        if (oneGoodsEntit.getData().getParam()!=null){
//            for (int i=0;i<oneGoodsEntit.getData().getParam().size();i++){
//                List<String> check=new ArrayList<>();
//                for (int j=0;j<oneGoodsEntit.getData().getParam().get(i).getParam_value().size();j++){
//                    check.add("0");
//                }
//                oneGoodsEntit.getData().getParam().get(i).setParam_check(check);
//            }
//
//        }

//        if (oneGoodsEntit.getData().getParam()==null){
//            Toast.makeText(getActivity(), "该商品信息还未完善", Toast.LENGTH_SHORT).show();
//        }else {
//            paramTypeAdapter.setImageList(oneGoodsEntit.getData().getParam());
//        }

        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });


        setBackgroundAlpha(0.5f);
        popWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getActivity().getWindow().setAttributes(lp);
    }

}
